#include "CPluginController.h"
#include "CPluginEnumerator.h"
#include "../obj/IGoodsPlugin.h"
#include "dlfcn.h"
#include <iostream>
#include <string.h>

using namespace std;

CPluginController::CPluginController(void)
{
}

CPluginController::~CPluginController(void)
{
}

bool CPluginController::InitializeController(std::string serverName)
{
	m_vpPlugin.clear();
	std::vector<std::string> vstrPluginNames;
	
	// 扫目录用
	CPluginEnumerator enumerator;
	cout << "插件控制器初始化" << endl;
	// 得到插件名称
	if (!enumerator.GetPluginNames(vstrPluginNames, serverName))
		return false;
	cout << "插件的个数" << vstrPluginNames.size() << endl;
	for (unsigned int i = 0; i < vstrPluginNames.size(); i++)
	{
		typedef int (*PLUGIN_CREATE)(IGoodsPlugin **);

		PLUGIN_CREATE CreateProc;
		IGoodsPlugin *pPlugin = NULL;

		void *hinstLib = dlopen(vstrPluginNames[i].c_str(), RTLD_LAZY);
		if (hinstLib != NULL)
		{
			m_vhForPlugin.push_back(hinstLib);

			CreateProc = (PLUGIN_CREATE)dlsym(hinstLib, "CreateObj");
			if (NULL != CreateProc)
			{
				(CreateProc)(&pPlugin);
				if (pPlugin != NULL)
				{
					m_vpPlugin.push_back(pPlugin);
				}
			}
		}
	}

	return true;
}

bool CPluginController::ProcessRequest(int FunctionID)
{

	for (unsigned int i = 0; i < m_vpPlugin.size(); i++)
	{
		if (m_vpPlugin[i]->GetID() == FunctionID)
		{
			m_vpPlugin[i]->Print();
			break;
		}
	}

	return true;
}

bool CPluginController::IfProcess(char *Function) //判断插件是否存在

{

	unsigned int i;

	for (i = 0; i < m_vpPlugin.size(); i++)
	{
		if (strcmp(Function, m_vpPlugin[i]->GetName()) == 0)
		{
			break;
		}
	};

	if (i < m_vpPlugin.size()) //插件存在
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool CPluginController::ProcessSerialize(int m_serverId)
{
	cout << "插件控制器进行序列化" << endl;
	FILE *fp = fopen("./serializeConfig.txt", "r+");
    char buf[100];
    int len;
    while(fgets(buf,100,fp) != NULL);
    {
		// 设置文件保存名
		sprintf(buf, "%s%d", buf, m_serverId);
        CLSerializer s;
		s.Serialize(buf, m_vpPlugin);
    }
	return true;
}

bool CPluginController::ProcessDeserialize(int m_serverId)
{
	cout << "插件控制器进行反序列化" << endl;
	FILE *fp = fopen("./serializeConfig.txt", "r+");
    char buf[100];
    int len;
    while(fgets(buf,100,fp) != NULL);
	// 3-1=2 3-2=1 就是1服务器找2，2服务器找1
	sprintf(buf, "%s%d", buf, 3 - m_serverId);
   	CLSerializer s;
	objLip l_lip;
    objMask l_mask;
	objBeer l_beer;
	objRedWine l_redWine;

    s.Register(&l_lip);
	s.Register(&l_mask);
	s.Register(&l_beer);
	s.Register(&l_redWine);

    std::vector<IGoodsPlugin*> v;
    s.Deserialize(buf, v);
    for(int i = 0; i < v.size(); i++)
    {
        objLip *p = dynamic_cast<objLip *>(v[i]);
        if(p != NULL)
		{
			for(int j = 0; j < m_vpPlugin.size(); j++)
			{
				if(p->GetID() == m_vpPlugin[j]->GetID())
				{
					m_vpPlugin[j] -> m_price = p -> m_price;
					m_vpPlugin[j] -> m_stock = p -> m_stock;
				}
			};
		}
	    objMask *q = dynamic_cast<objMask *>(v[i]);
        if(q != NULL)
		{
			for(int j = 0; j < m_vpPlugin.size(); j++)
			{
				if(q->GetID() == m_vpPlugin[j]->GetID())
				{
					m_vpPlugin[j] -> m_price = q -> m_price;
					m_vpPlugin[j] -> m_stock = q -> m_stock;
				}
			};
		}
	    objBeer *s = dynamic_cast<objBeer *>(v[i]);
        if(s != NULL)
		{
			for(int j = 0; j < m_vpPlugin.size(); j++)
			{
				if(s->GetID() == m_vpPlugin[j]->GetID())
				{
					m_vpPlugin[j] -> m_price = s -> m_price;
					m_vpPlugin[j] -> m_stock = s -> m_stock;
				}
			};
		}
	    objRedWine *t = dynamic_cast<objRedWine *>(v[i]);
        if(t != NULL)
		{
			for(int j = 0; j < m_vpPlugin.size(); j++)
			{
				if(t->GetID() == m_vpPlugin[j]->GetID())
				{
					m_vpPlugin[j] -> m_price = t -> m_price;
					m_vpPlugin[j] -> m_stock = t -> m_stock;
				}
			};
		}
    }
	return true;
}

bool CPluginController::UninitializeController()
{

	for (unsigned int i = 0; i < m_vhForPlugin.size(); i++)
	{
		dlclose(m_vhForPlugin[i]);
	}
	return true;
}

bool CPluginController::ProcessGetAllId(char *FunctionList[], int *num)
{
	num = 0;
	for (unsigned int i = 0; i < m_vpPlugin.size(); i++)
	{
		FunctionList[i] = m_vpPlugin[i]->GetName();
		num += 1;
	}
	return true;
}

bool CPluginController::ProcessSelectById(char *Function, int *price, int *stock)
{
	for (unsigned int i = 0; i < m_vpPlugin.size(); i++)
	{
		if (strcmp(Function, m_vpPlugin[i]->GetName()) == 0)
		{
			*price = m_vpPlugin[i]->getPrice();
			*stock = m_vpPlugin[i]->getStock();
			break;
		}
	}
	return true;
}

bool CPluginController::ProcessInsertById(char *Function, int num)
{
	for (unsigned int i = 0; i < m_vpPlugin.size(); i++)
	{
		if (strcmp(Function, m_vpPlugin[i]->GetName()) == 0)
		{
			m_vpPlugin[i]->m_stock = m_vpPlugin[i]->getStock() + num;
			break;
		}
	}
	return true;
}

bool CPluginController::ProcessDelineById(char *Function, int num)
{
	for (unsigned int i = 0; i < m_vpPlugin.size(); i++)
	{
		if (strcmp(Function, m_vpPlugin[i]->GetName()) == 0)
		{
			if (m_vpPlugin[i]->getStock() < num) //库存不足
				return false;
			m_vpPlugin[i]->m_stock = m_vpPlugin[i]->getStock() - num;
			break;
		}
	}
	return true;
}

bool CPluginController::ProcessUpdatePriceById(char *Function, int newPrice)
{
	for (unsigned int i = 0; i < m_vpPlugin.size(); i++)
	{
		if (strcmp(Function, m_vpPlugin[i]->GetName()) == 0)
		{
			if (newPrice <= 0)
				return false;
			m_vpPlugin[i]->m_price = newPrice;
			break;
		}
	}
	return true;
}

// 将控制器对象与server对象绑定
bool CPluginController::getPlugins(vector<IGoodsPlugin *> *vpPlugin)
{
	std::cout << "getPlugins插件个数:" << m_vpPlugin.size() << std::endl;
	// 把控制器里的插件传给server的对象
	*vpPlugin = m_vpPlugin;
	return true;
}

bool CPluginController::setPlugins(vector<IGoodsPlugin *> *vpPlugin)
{
	std::cout << "setPlugins插件个数:" << (*vpPlugin).size() << std::endl;
	// 把控制器里的插件传给server的对象
	m_vpPlugin = *vpPlugin;
	return true;
}